package com.mt.roll.common.controller;

import com.mt.roll.common.params.DownloadFileParam;
import com.mt.roll.common.params.UploadFileBase64Param;
import com.mt.roll.common.service.AttachmentService;
import com.mt.roll.common.vo.SysFileVo;
import com.mt.roll.core.base.BaseController;
import com.mt.roll.core.constants.CommonConstant;
import com.mt.roll.core.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author jiff
 * @date 2021/01/12
 * @since 1.0
 */
@RestController
@RequestMapping(CommonConstant.API_MODULE_COMMON + "attachment")
@Slf4j
@Api(value = "附件管理", tags = "附件管理", hidden = false)
public class AttachmentController extends BaseController {
    @Autowired
    private AttachmentService attachmentService;


    @PostMapping(value = "uploadFile")
    @ApiOperation(value = "上传文件", notes = "上传文件")
    public R<List<SysFileVo>> uploadFile(@ApiParam(value = "文件类型", required = true) @NotEmpty(message = "文件类型编码不得为空") @RequestParam String typeCode) {
        return attachmentService.uploadFile(typeCode);
    }

    @PostMapping("uploadFileBase64")
    @ApiOperation(value = "文件上传(base64字符串方式)", notes = "文件上传")
    public R<SysFileVo> uploadFileBase64(@Validated @RequestBody UploadFileBase64Param param) {
        return attachmentService.uploadFileBase64(param);
    }

    @PostMapping("uploadFileFormData")
    @ApiOperation(value = "文件上传(form表单形式)", notes = "文件上传")
    public R<List<SysFileVo>> uploadFileFormData(@ApiParam(value = "文件类型", required = true) @NotEmpty(message = "文件类型编码不得为空") @RequestParam String typeCode, @ApiParam(value = "文件附件", required = true) @NotNull(message = "文件附件不能为空") @RequestParam("files") MultipartFile[] files) {
        return attachmentService.uploadFileFormData(typeCode, files);
    }

    @GetMapping(value = "downloadFile")
    @ApiOperation(value = "下载文件", notes = "下载文件")
    public R<Object> downloadFile(@Validated DownloadFileParam param) {
        return attachmentService.downloadFile(param);
    }


}
